// pages/title/title.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items:{},
    id:"0",//当前题目id
    answer: ["cuo", "cuo", "cuo", "cuo","cuo"],//当前答案
    state: ["none", "none", "none", "none", "none"],
    analyze:"none",
    laststate: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let apiname=this.apichange(options.id);
    wx.request({
      url: `${app.globalData.apiurl}${apiname}/list`,
      method:"GET",
      data:{
        account:10
      },
      success:res=>{
        console.log(res)
        let count=res.data.length
        
        for(let i=0;i<count;i++){
          let ans = res.data[i].answer.split(",")
          console.log(ans)
          let aann = ["cuo", "cuo", "cuo", "cuo", "cuo"]
          for(let j=0;j<ans.length;j++){
            if (ans[j] == "A") {
              aann[0] = "dui"
            }
            if (ans[j] == "B") {
              aann[1] ="dui"
            }
            if (ans[j] == "C") {
              aann[2] = "dui"
            }
            if (ans[j] == "D") {
              aann[3] = "dui"
            }
            if (ans[j] == "D") {
              aann[4] = "dui"
            }
          }
          res.data[i].answer=aann;
        }
        this.setData({
          items: res.data,
          answer: res.data[this.data.id].answer
        })
        console.log(this.data.items[this.data.id])
      }
    })
   
  },
  //下一题点击事件
  bindNext: function (e) {
    let state=this.data.state;
    state = ["none", "none", "none", "none","none"];
    this.setData({
      state:state,
      analyze:"none",
      laststate: "",
      touch:0
    })
    if(this.data.id==9){
      wx.showToast({
        title: '已经做完10题！点左上角返回！',
        icon:"none"
      })
      return false;
    }else{
      let ide=this.data.id*1+1
      console.log(ide)
      this.setData({
        id:ide,
        answer:this.data.items[ide].answer
      })
      console.log(this.data.items[this.data.id])
    }
  },
  //上一题点击事件
  bindLast: function (e) {
    let state = this.data.state;
    state = ["none", "none", "none", "none"];
    this.setData({
      state: state,
      analyze: "none",
      touch:0
    })
    if (this.data.id == 0) {
      wx.showToast({
        title: '无上一题！',
        icon: "none"
      })
      return false;
    } else {
      let ide = this.data.id * 1 - 1
      this.setData({
        id: ide,
        answer: this.data.items[ide].answer,
        touch: 0
      })
      if (ide == 0) {
        this.setData({
          laststate: ""
        })
      }
      console.log(this.data.items[this.data.id])
    }
  },
  //选项点击事件
  chance:function(e){
    console.log(e.currentTarget.id)
    if (this.data.touch == 1) {
      return false;
    }
    if (this.data.answer[e.currentTarget.id] =="cuo"){
      this.setData({
        analyze:"",
        touch:1
      })
    }
    if (this.data.state[e.currentTarget.id]=="none"){
      let state=this.data.state;
      state[e.currentTarget.id]="";
      this.setData({
        state:state
      })
    }else{
      let state = this.data.state;
      state[e.currentTarget.id] = "none";
      this.setData({
        state: state
      })
    }
  },
  //api名称转换
  apichange:function(id){
    if (id == 43) {
      let name = "zsbYxzhDoctorx"
      return name;
    }
    // if (id == 42) {
    //   let name = "zsbYyOption"
    //   return name;
    // }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})